package com.wzy.mall.web.servlet.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wzy.mall.model.bo.clientBO.OrderAddBO;
import com.wzy.mall.model.bo.clientBO.OrderCartSettleListBO;
import com.wzy.mall.model.bo.clientBO.OrderSendCommentBO;
import com.wzy.mall.model.vo.ResultVO;
import com.wzy.mall.model.vo.clientVO.OrderCartShowVO;
import com.wzy.mall.service.client.OrderService;
import com.wzy.mall.service.client.impl.OrderServiceImpl;
import com.wzy.mall.utils.HTTPUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class Order {

    private static ObjectMapper jsonMapper = new ObjectMapper();
    private static OrderService orderService = new OrderServiceImpl();

    /**
     *  添加订单
     * @param request
     * @param response
     */
    public static void addOrder(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 获取JSON数据
            String requestBody = HTTPUtils.getJSONRequest(request);
            // 封装成BO对象
            OrderAddBO orderAddBO = jsonMapper.readValue(requestBody, OrderAddBO.class);

            // 调用Service层
            int result = orderService.addOrder(orderAddBO);
            if(result == 0) {
                ResultVO error = ResultVO.error("服务器闹别扭，订单提交失败啦！\r\n等会再试试吧");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else if(result == 601) {
                // 库存量不足
                ResultVO error = ResultVO.error("库存量不足啦！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else if(result == 505) {
                // 库存量修改失败
                ResultVO error = ResultVO.error("服务器过载，稍后再试吧！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(null);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  根据state和token获取订单信息
     * @param request
     * @param response
     */
    public static void getOrderByState(HttpServletRequest request, HttpServletResponse response) {
        // 获取键值对
        // state
        String state = request.getParameter("state");
        // token
        String token = request.getParameter("token");

        // 调用Service层
        List<OrderCartShowVO> orderCartShowVOList = orderService.getOrderByState(state, token);
        try {
            if(orderCartShowVOList == null) {
                ResultVO error = ResultVO.error("服务器闹别扭中，请勿打扰");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(orderCartShowVOList);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     *  购物车下单结算
     * @param request
     * @param response
     */
    public static void settleAccounts(HttpServletRequest request, HttpServletResponse response) {

        try {
            // 获取JSON数据
            String requestBody = HTTPUtils.getJSONRequest(request);

            // 封装成BO对象
            OrderCartSettleListBO orderCartSettleListBO = jsonMapper.readValue(requestBody, OrderCartSettleListBO.class);
            //System.out.println(orderCartSettleListBO);

            // 调用Service
            int result = orderService.settleAccounts(orderCartSettleListBO);

            if(result == 0) {
                ResultVO error = ResultVO.error("提交失败啦！等会再试试吧？");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else if(result == 601) {
                // 库存量不足
                ResultVO error = ResultVO.error("库存量不足啦！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else if(result == 505) {
                // 库存量修改失败
                ResultVO error = ResultVO.error("服务器过载，稍后再试吧！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(null);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  购物车付款
     * @param request
     * @param response
     */
    public static void pay(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        // Service
        int result = orderService.updateOrderState(Integer.parseInt(id));

        try {
            if(result == 0) {
                ResultVO error = ResultVO.error("付款失败啦！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else if(result == 601) {
                // 库存量不足
                ResultVO error = ResultVO.error("库存量不足啦！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else if(result == 505) {
                // 库存量修改失败
                ResultVO error = ResultVO.error("服务器过载，稍后再试吧！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(null);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  删除订单（逻辑删除 —— 更新订单表的visible属性）
     * @param request
     * @param response
     */
    public static void deleteOrder(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        int result = orderService.deleteOrder(Integer.parseInt(id));

        try {
            if(result == 0) {
                ResultVO error = ResultVO.error("删除失败啦啦啦");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(null);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  确认收货 —— 更改订单状态
     * @param request
     * @param response
     */
    public static void confirmReceive(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        int result = orderService.confirmReceive(Integer.parseInt(id));

        try {
            if(result == 0) {
                ResultVO error = ResultVO.error("提交失败啦啦啦");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(null);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  提交商品评价
     * @param request
     * @param response
     */
    public static void sendComment(HttpServletRequest request, HttpServletResponse response) {
        try {
            String requstBody = HTTPUtils.getJSONRequest(request);
            // BO
            OrderSendCommentBO orderSendCommentBO = jsonMapper.readValue(requstBody, OrderSendCommentBO.class);

            int result = orderService.insertComment(orderSendCommentBO);
            try {
                if(result == 0) {
                    ResultVO error = ResultVO.error("肥肠抱歉，评价提交失败鸟");
                    response.getWriter().write(jsonMapper.writeValueAsString(error));
                } else {
                    ResultVO ok = ResultVO.ok(null);
                    response.getWriter().write(jsonMapper.writeValueAsString(ok));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
